Skip to content

fix: normalize function argument types to prevent spurious replacements#158

Closed
p-c-h-b wants to merge 1 commit intopgplex:mainfrom
p-c-h-b:fix/function-replacement
Closed

fix: normalize function argument types to prevent spurious replacements#158
p-c-h-b wants to merge 1 commit intopgplex:mainfrom
p-c-h-b:fix/function-replacement

Conversation

@p-c-h-b
Copy link
Contributor

@p-c-h-b p-c-h-b commented Nov 8, 2025

This fixes a bug where function signature comparison would incorrectly treat functions as different when PostgreSQL stores parameter types with schema qualifications while the source SQL uses unqualified names.

Problem

When PostgreSQL stores a function, it may qualify custom type names with their schema (e.g., public.user_status). However, source SQL often uses unqualified type names (e.g., user_status).

When pgschema compares the old and new function signatures:

  1. Old function (from database): my_func(public.user_status)
  2. New function (from SQL files): my_func(user_status)

pgschema would treat these as different functions and attempt to:

DROP FUNCTION my_func(public.user_status);
CREATE OR REPLACE FUNCTION my_func(user_status) ...;

The DROP would fail if other objects (views, triggers, other functions) referenced my_func, even though CREATE OR REPLACE would have worked fine.

Solution

Added signature normalization in ir/ir.go:

  1. New helper function stripSchemaPrefixFromType(typeName, schema) that removes schema prefixes from type names when they match the function's own schema:

    • Handles quoted schema qualifications: "public".user_statususer_status
    • Handles unquoted qualifications: public.user_statususer_status
    • Preserves cross-schema references: other_schema.typeother_schema.type
  2. Modified Function.GetArguments() to normalize all parameter types before building the signature string used for comparison.

This ensures that public.user_status and user_status are treated as identical when comparing function signatures, allowing CREATE OR REPLACE to work instead of attempting a DROP.

Changes

  • Added stripSchemaPrefixFromType() helper function
  • Modified Function.GetArguments() to apply normalization to all parameter types

Fixes #155 (partially - this is the third of three fixes mentioned in the issue)

This fixes a bug where function replacements would fail because PostgreSQL
stores parameter types with schema qualifications (e.g., 'public.user_status')
while the source SQL might use unqualified names (e.g., 'user_status').

When comparing function signatures, pgschema would treat these as different
functions and try to DROP the old one before CREATE OR REPLACE, causing
failures when other objects referenced the function.

Changes:
- Added stripSchemaPrefixFromType() helper to normalize type names in
  function arguments by removing schema prefixes that match the function's
  schema
- Modified Function.GetArguments() to strip schema prefixes from all
  parameter types before building the argument signature
- This ensures 'public.user_status' and 'user_status' are treated as
  identical when comparing function signatures

Fixes pgplex#155 (partially - this is the third of three fixes)
Copilot AI review requested due to automatic review settings November 8, 2025 03:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds schema prefix normalization for function parameter types to ensure stable function identification across PostgreSQL schema-qualified type names. The main purpose is to strip matching schema prefixes from parameter types when building function argument strings.

  • Added stripSchemaPrefixFromType helper function to normalize schema-qualified type names
  • Modified GetArguments method to use the new normalization function
  • Applied consistent formatting to struct field alignments and removed extraneous blank lines

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianzhou tianzhou requested a review from Copilot November 8, 2025 03:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianzhou
Copy link
Contributor

tianzhou commented Nov 9, 2025

@p-c-h-b I developed a new fix and added test case in #160. So closing this one.

Thanks for reporting and taking the initiative.

@tianzhou tianzhou closed this Nov 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix dependency handling: FK ordering, schema normalization, and function replacements

3 participants